﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Data;
using Common.Library;
using OnlineVideos.Common;
using OnlineVideos.UI;
using OnlineVideos.AudioPlaybackAgent;
using OnlineVideos.Library;
using OnlineVideos.View_Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace OnlineVideos
{
    public partial class ShowDetail : UserControl
    {   
        #region Global
        ShowDetail detailModel;
         #endregion
        public ShowDetail()
        {
            InitializeComponent();
        }
        private void LoadDescriptionSection(long showID)
        {
            Show objMovieDetails = OnlineShow.GetShowDetail(showID);
            tblRelease.Text = objMovieDetails.RelaseDate;
            tblkTitle.Text = objMovieDetails.Title;
            rate.Rating = Convert.ToInt32(objMovieDetails.Rating);
            //RatingImage.Source = objMovieDetails.ImgRating;
            if (!string.IsNullOrEmpty(objMovieDetails.Description))
            {
                tblkDescription.Text = objMovieDetails.Description;
            }
            //TODO: Probably need to add relative path prefix.
            imgMovie.Source = ImageHelper.GetBitmapFromPath(objMovieDetails.Image);
            imgShare.Tag = objMovieDetails.Title;
            imgFav.Source = ResourceHelper.getShowFavoriteStatusImage(objMovieDetails.Favorite);
        }
        private void rate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgShare_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = ResourceHelper.ProjectName + " App";
            shareLinkTask.Message = " Watch bollywood movie '" + imgShare.Tag + "', Get the app at \n";
            shareLinkTask.LinkUri = ResourceHelper.AppMarketplaceWebLink;
            shareLinkTask.Show();
        }

        private void imgFav_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           FavoritesManager.SaveFavorites(AppSettings.ShowUniqueID);

            if (FavoritesManager.IsFavoriteShow(AppSettings.ShowUniqueID))
                imgFav.Source = ResourceHelper.RemoveFromFavoritesImage;
            else
                imgFav.Source = ResourceHelper.AddToFavoritesImage;
        }

        private void imgpin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            detailModel.PinOrUnpinShow(imgPinUnpin);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDescriptionSection(AppSettings.ShowUniqueID);
        }
        public void PinOrUnpinShow(Image PinUnpinIcon)
        {
            bool IsMoviePinned;

            Show show = OnlineShow.GetShowDetail(Convert.ToInt32(AppSettings.ShowID));

            IsMoviePinned = ShellTileHelper.IsPinned(AppSettings.ShowID);
            if (IsMoviePinned)
            {
                ShellTileHelper.UnPin(AppSettings.ShowID);
                Storage.DeleteSecondaryTileImage(show.Image);
                if (PinUnpinIcon != null)
                    PinUnpinIcon.Source = ResourceHelper.PinImage;
            }
            else
            {
                ShellTileHelper.PinShowToStartScreen(show);

                if (PinUnpinIcon != null)
                    PinUnpinIcon.Source = ResourceHelper.UnpinImage;
            }
        }
    }
}
